﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Serilog;
namespace Admin2024.Infrastructure.Middleware
{
    public class RequestHeaderMiddleware 
    {
        private readonly RequestDelegate _next;

      
        public RequestHeaderMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task InvokeAsync(HttpContext context)
        {

            // 获取请求头中的Authorization令牌
            var token = context.Request.Headers["Authorization"].FirstOrDefault();

            if (!string.IsNullOrEmpty(token))
            {
                // 通常令牌会以"Bearer "开头，需要去除这个前缀
                token = token.TrimStart().Replace("Bearer ", "", StringComparison.OrdinalIgnoreCase);
            }
            
            await _next(context);
            
        }
    }
}
